package com.hunt.app.controller;


import com.hunt.dao.TransactionCalendarMapper;
import com.hunt.model.app.FncWarning;
import com.hunt.model.app.FncWarningResults;
import com.hunt.model.app.TransactionCalendar;
import com.hunt.service.app.FncWarningService;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: ww 股东查询
 * @Date : 2020/10/19
 */

@Api(value = "/app/pc端指标预警查询")
@Controller
@RequestMapping(path = "/app/pc/fncWarning")
public class FncWarningController {

    private static final Logger log = LoggerFactory.getLogger(FncWarningController.class);
    @Autowired
    private FncWarningService fncWarningService;
    @Autowired
    private TransactionCalendarMapper transactionCalendarMapper;
    @ApiOperation(value = "查询最新指标预警", httpMethod = "GET",  produces = "application/json;charset=utf-8", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findNewFncWarning", method = RequestMethod.GET)
    public Result findNewFncWarning(@ApiParam(value = "dfp是多方炮,sdgz_zx是四点共振中线,yycdx是一阳穿多线,zczx是早晨之星,cxyx是长下影线,sdgz_zx是四点共振短线")@RequestParam(value = "type") String type,
                                    @RequestParam(value = "position",defaultValue = "0") Integer position,
                                    @RequestParam(value = "size",defaultValue = "15") Integer size){
        return fncWarningService.findNewFncWarning(type,position,size);
    }
    @ApiOperation(value = "查询最新指标预警备用", httpMethod = "GET",  produces = "application/json;charset=utf-8", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findNewFncWarningSpare", method = RequestMethod.GET)
    public Result findNewFncWarningSpare(@ApiParam(value = " dxfz是岛形反转形,dtpl是多头排列,dkdyx是低开大阳线,xrzl是仙人指路,qszl是强势整理形态,flsg是突破首版,jsj是金山角,ldjc是六大金叉 ")@RequestParam(value = "type") String type,
                                    @RequestParam(value = "position",defaultValue = "0") Integer position,
                                    @RequestParam(value = "size",defaultValue = "15") Integer size,
                                         @RequestParam(value = "data",defaultValue = "0") String date,
                                         @RequestParam(value = "sort",defaultValue = "1") String sort
                                         ) throws ParseException {
        Date todaydate=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String todaydatestr=simpleDateFormat.format(todaydate);
        //0代表默认当天
        if("0".equals(date)){
            date=simpleDateFormat.format(todaydate);
            if(!transactionCalendarMapper.findByCalDate(date).getIs_open().equals("1")){
                date=transactionCalendarMapper.getbefor(date);
                //1代表默认降序
                return fncWarningService.findNewFncWarningSpareByData(type,position,size, date,sort);
            }else {
                return fncWarningService.findNewFncWarningSpare(type,position,size,sort);
            }
        }else {
            if(isafter(date)){
                FncWarningResults fncWarnings = new FncWarningResults();
                return  Result.success(fncWarnings);
            }
            if(!transactionCalendarMapper.findByCalDate(date).getIs_open().equals("1")){
                FncWarningResults fncWarnings = new FncWarningResults();
                return  Result.success(fncWarnings);
            }
            if(date.equals(todaydatestr)){
                return fncWarningService.findNewFncWarningSpare(type,position,size,sort);
            }
            else {
                return fncWarningService.findNewFncWarningSpareByData(type,position,size, date,sort);
            }
        }

    }
    private  boolean  isafter(String date) throws ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        Date checkdate=  simpleDateFormat.parse(date);
       Date todaydate=new Date();
       if(checkdate.after(todaydate)){
           return  true;
       }
       else {
           return  false;
       }

    }
}
